added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSCallNativeDllWrapper / Program.cs
blob18d12b7ba070b7628eb67a431cbc6beeba5d1328
1 /****************************** Module Header ******************************\
2 Module Name: Program.cs
3 Project: CSCallNativeDllWrapper
4 Copyright (c) Microsoft Corporation.
6 The code sample demonstrates calling the functions and classes exported by a
7 native C++ DLL from Visual C# code through C++/CLI wrapper classes.
9 CSCallNativeDllWrapper (this .NET application)
10 -->
11 CppCLINativeDllWrapper (the C++/CLI wrapper)
12 -->
13 CppDynamicLinkLibrary (a native C++ DLL module)
15 This source is subject to the Microsoft Public License.
16 See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
17 All other rights reserved.
19 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
20 EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
22 \***************************************************************************/
24 using System;
25 using System.Runtime.InteropServices;
26 using CppCLINativeDllWrapper;
29 namespace CSCallNativeDllWrapper
31 class Program
33 static void Main(string[] args)
35 bool isLoaded = false;
36 const string moduleName = "CppDynamicLinkLibrary";
38 // Check whether or not the module is loaded.
39 isLoaded = IsModuleLoaded(moduleName);
40 Console.WriteLine("Module \"{0}\" is {1}loaded", moduleName, isLoaded ? "" : "not ");
42 // Call the functions exported from the module.
44 string str = "HelloWorld";
45 int length;
47 length = NativeMethods.GetStringLength1(str);
48 Console.WriteLine("GetStringLength1(\"{0}\") => {1}", str, length);
50 length = NativeMethods.GetStringLength2(str);
51 Console.WriteLine("GetStringLength2(\"{0}\") => {1}", str, length);
54 // Call the callback functions exported from the module.
56 CompareCallback cmpFunc = new CompareCallback(CompareInts);
57 int max = NativeMethods.Max(2, 3, cmpFunc);
59 // Make sure the lifetime of the delegate instance covers the
60 // lifetime of the unmanaged code; otherwise, the delegate
61 // will not be available after it is garbage-collected, and
62 // you may get the Access Violation or Illegal Instruction
63 // error.
64 GC.KeepAlive(cmpFunc);
65 Console.WriteLine("Max(2, 3) => {0}", max);
68 // Use the class exported from the module.
70 CSimpleObjectWrapper obj = new CSimpleObjectWrapper();
71 obj.FloatProperty = 1.2F;
72 float fProp = obj.FloatProperty;
73 Console.WriteLine("Class: CSimpleObject::FloatProperty = {0:F2}", fProp);
76 // You cannot unload the C++ DLL CppDynamicLinkLibrary by calling
77 // GetModuleHandle and FreeLibrary.
79 // Check whether or not the module is loaded.
80 isLoaded = IsModuleLoaded(moduleName);
81 Console.WriteLine("Module \"{0}\" is {1}loaded", moduleName, isLoaded ? "" : "not ");
85 /// <summary>
86 /// This is the callback function for the method Max exported from
87 /// the DLL CppDynamicLinkLibrary.dll.
88 /// </summary>
89 /// <param name="a">the first integer</param>
90 /// <param name="b">the second integer</param>
91 /// <returns>
92 /// The function returns a positive number if a > b, returns 0 if a
93 /// equals b, and returns a negative number if a &lt b.
94 /// </returns>
95 static int CompareInts(int a, int b)
97 return (a - b);
101 #region Module Related Helper Functions
103 /// <summary>
104 /// Check whether or not the specified module is loaded in the
105 /// current process.
106 /// </summary>
107 /// <param name="moduleName">the module name</param>
108 /// <returns>
109 /// The function returns true if the specified module is loaded in
110 /// the current process. If the module is not loaded, the function
111 /// returns false.
112 /// </returns>
113 static bool IsModuleLoaded(string moduleName)
115 // Get the module in the process according to the module name.
116 IntPtr hMod = GetModuleHandle(moduleName);
117 return (hMod != IntPtr.Zero);
120 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
121 static extern IntPtr GetModuleHandle(string moduleName);
123 #endregion